package com.neusoft.controller;

import com.alibaba.fastjson.JSONObject;
import com.neusoft.po.CommonResult;
import com.neusoft.po.User;
import com.neusoft.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

@RefreshScope
@RestController
@RequestMapping("/UserController")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/getUserByIdByPass/{userId}/{password}")
    public CommonResult<JSONObject> getUserByIdByPass(
            @PathVariable("userId") String userId,
            @PathVariable("password") String password) throws Exception {
        User user = new User();
        user.setUserId(userId);
        user.setPassword(password);

        JSONObject result = userService.getUserByIdByPass(user);
        return new CommonResult(200, "success", result);
    }

    @GetMapping("/getUserById/{userId}")
    public CommonResult<User> getUserById(@PathVariable("userId") String userId) throws Exception {
        User user = userService.getUserById(userId);
        return new CommonResult(200, "success", user);
    }

    @PostMapping("/saveUser/{userId}/{password}/{userName}/{userSex}/{userImg}")
    public CommonResult<Integer> saveUser(
            @PathVariable("userId") String userId,
            @PathVariable("password") String password,
            @PathVariable("userName") String userName,
            @PathVariable("userSex") Integer userSex,
            @PathVariable("userImg") String userImg) throws Exception {
        User user = new User();
        user.setUserId(userId);
        user.setPassword(password);
        user.setUserName(userName);
        user.setUserSex(userSex);
        user.setUserImg(userImg);
        user.setDelTag(1);

        int result = userService.saveUser(user);
        return new CommonResult(200, "success", result);
    }

    @PutMapping("/updateUserById/{userId}/{password}/{userName}/{userSex}/{userImg}/{delTag}")
    public CommonResult<Integer> updateUserById(
            @PathVariable("userId") String userId,
            @PathVariable("password") String password,
            @PathVariable("userName") String userName,
            @PathVariable("userSex") String userSex,
            @PathVariable("userImg") String userImg,
            @PathVariable("delTag") String delTag) throws Exception {
        User user = new User();
        user.setUserId(userId);
        user.setPassword(password);
        user.setUserName(userName);
        user.setUserSex(Integer.getInteger(userSex));
        user.setUserImg(userImg);
        user.setDelTag(Integer.getInteger(delTag));

        int result = userService.updateUserById(user);
        return new CommonResult(200, "success", result);
    }

}
